import sys
sys.path.append("../utils")
config_path = "./config.cfg"

import ConfigParser
import time
import datetime as dt

import matplotlib.pyplot as plt
import tables as tb
import numpy as np

import utilsabbo as util
import data_utilsabbo as dutil

alarm_years = 2006, 2007, 2008, 2009, 2010, 2011

def get_alarms_data_from_csv(year):
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    csv_path = config.get('options', 'csv_path')
    alarm_type = "active-alarm-no-"
    alarm_name = "ActiveAlarmNo"
    fil_str = csv_path + alarm_type + str(year) + ".csv"
    f = open(fil_str, 'r')
    name_list = f.readline()\
                            .replace('\n', '')\
                            .replace('\r', '')\
                            .replace('-', '')\
                            .split(';')
    info_str = f.readline().replace('\n', '').replace('\r', '').split(';')
    tid = f.readline().replace('\n', '').replace('\r', '').split(';')[0]
    f.close()
    #print info_str
    #print tid
    num_of_meas = len(name_list)
    c = np.arange(0, num_of_meas, 2)
    # Cleaning the namelist
    new_name = []
    turbines = []
    for name in name_list:
        if len(name) > 0:
            new_name.append(name)
            turbines.append(name[0:6])
            alarm_type = name[6:]
    name_list = new_name
    dtyp = []
    for pos in c:
        time_label = name_list[pos] + 'Time'
        name_list.insert(pos, time_label)
        info_str[pos] = name_list[pos + 1]
        dtyp.append(float)
        dtyp.append('|O4')

    info = dict(zip(info_str[::2], info_str[1::2]))
    construct_db_class2()
    if len(tid) > 15:
        convs = dict((col, util.mkdatess2) for col in c)
    else:
        convs = dict((col, util.mkdate2) for col in c)
    data = np.genfromtxt(fil_str,
                         delimiter=";",
                         names=name_list,
                         skip_header=2,
                         converters=convs,
                         dtype=dtyp)

    #print info.keys()
    #print len(info.keys())
    #print type(data)
    #print data
    #dfs = []
    for turbine in turbines:
        num = int(info[turbine + alarm_name])
        num = int(info[turbine + alarm_name])
        alarms = np.array([data[turbine + alarm_name][0:num]]).T
        times = np.array([data[turbine + alarm_name + "Time"][0:num]]).T
        dat = np.hstack((alarms, times))
        write_to_h5_2(turbine, dat)
        #alarms = data[turbine + "AlarmTxt"][0:num]
        #times = data[turbine + "AlarmTxtTime"][0:num]
        #write_to_h5_pd(turbine, dat)
        #dfs.append(get_pd_df(turbine, alarms, times))
    #write_to_h5_pd(dfs)

def construct_db_class():
    """
    This functions generates a file with the class
    used for constructing the PyTables database
    """
    f = open("hest.py", 'w')
    f.write("import tables as tb\n")
    f.write("class Particle(tb.IsDescription):\n")
    f.write(("    " + "AlarmTxt" + " = tb.StringCol(itemsize=100, shape=(), dflt='', pos=0)\n"))
    f.write(("    " + "Time" + " = tb.Float64Col(pos=1)\n"))
    f.close()
    return

def construct_db_class2():
    """
    This functions generates a file with the class
    used for constructing the PyTables database
    """
    f = open("hest2.py", 'w')
    f.write("import tables as tb\n")
    f.write("class Particle(tb.IsDescription):\n")
    f.write(("    " + "AlarmTxt" + " = tb.StringCol(itemsize=100, shape=(), dflt='', pos=0)\n"))
    f.write(("    " + "Time" + " = tb.Float64Col(pos=1)\n"))
    f.write(("    " + "Turbine" + " = tb.StringCol(itemsize=6, shape=(), dflt='', pos=2)\n"))
    f.close()
    return

import pandas as pd
def get_pd_df(turbine, alarms, times):
    df = pd.DataFrame.from_items([('Time', times), ("Alarm", alarms)])
    df['Turbine'] = turbine
    return df


def write_to_h5_pd(dfs):
    store = pd.HDFStore('store.h5')
    try:
        for df in dfs[:]:
            store['df'] = store['df'].append(df, ignore_index=True)
    except:
        print "new"
        store['df'] = dfs[0]
        for df in dfs[1:]:
            store['df'] = store['df'].append(df, ignore_index=True)
    return


def write_to_h5(turbine, data):
    import hest as db_cl
    filters = tb.Filters(complevel=2, complib="blosc")
    h5file = "alarms2.h5"
    h5file = tb.openFile((h5file), mode='a',
            title="Alarm Data", filters=filters)
    group = h5file.root

    if turbine in h5file.root._v_children.keys():
        table = h5file.root._v_children[turbine]
    else:
        table = h5file.createTable(group, turbine, db_cl.Particle)
    particle = table.row
    for dat in data:
        particle['AlarmTxt'] = dat[0]
        particle['Time'] = dat[1]
        particle.append()
    table.flush()
    h5file.close()

    return

def write_to_h5_2(turbine, data):
    import hest2 as db_cl
    comp_filter = tb.Filters(complevel=2, complib="blosc")
    h5file = "alarms.h5"
    h5file = tb.openFile((h5file), mode='a', title="Alarm Data", filters=comp_filter)
    group = h5file.root

    if "data" in h5file.root._v_children.keys():
        table = h5file.root._v_children["data"]
    else:
        table = h5file.createTable(group, "data", db_cl.Particle)
    particle = table.row
    for dat in data:
        particle['AlarmTxt'] = dat[0]
        particle['Time'] = dat[1]
        particle['Turbine'] = turbine
        particle.append()
    table.flush()
    h5file.close()

    return


def main():
    start_time = time.time()
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    for year in alarm_years:
        print year
        get_alarms_data_from_csv(year)

    print "Done in:",
    print (time.time() - start_time)
    return

if __name__=='__main__':
    main()

